package org.lisen.controlleradvice.handler;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 * @author laughing
 * @date 2020/9/26
 * @site https://www.lisen.org
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理越界异常
     * @param indexOutOfBoundsException 越界异常
     * @return Map
     */
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Map<String,Object> indexOutOfBoundsException(IndexOutOfBoundsException indexOutOfBoundsException){
        Map<String,Object> map = new HashMap<>(2);
        map.put("code",400);
        map.put("msg","越界异常");
        return map;
    }

    /**
     * 处理空引用异常
     * @param nullPointerException 空引用
     * @return Map
     */
    @ExceptionHandler(NullPointerException.class)
    public Map<String,Object> nullPointerException(NullPointerException nullPointerException){
        Map<String,Object> map = new HashMap<>(2);
        map.put("code",400);
        map.put("msg","空引用异常");
        return map;
    }


}
